package com.person.sys.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.person.base.model.BaseRole;
import com.person.commons.utils.DAO;
import com.person.sys.model.SysFunc;
import com.person.sys.model.SysMenu;
import com.person.sys.model.SysMenuNode;
import com.person.sys.service.SysFuncService;

public class SysFuncServiceImpl implements SysFuncService {
	private DAO dao;
	private static final char SUCCESS = '1';
	private static final char FAIL = '0';
	private static final char FAIL_1 = '2';
	private static final char FAIL_2 = '3';
	private static final char FAIL_3 = '4';
	private static final char FAIL_4 = '5';
	private static final char FAIL_5 = '6';
	
	public DAO getDao() {
		return dao;
	}

	public void setDao(DAO dao) {
		this.dao = dao;
	}

	@Override
	public char createSysFunc(SysFunc sysFunc)throws Exception {
		
		String hql1 = "from SysFunc where menuCodeGroup ='"+sysFunc.getMenuCodeGroup()+"'";
		String hql2 = "from SysFunc where funcName ='"+sysFunc.getFuncName()+"'";
		String hql3 = "from SysFunc where funcCode ='"+sysFunc.getFuncCode()+"'";
		String hql4 = "from SysFunc where funcNameEn ='"+sysFunc.getFuncNameEn()+"'";
		List list1 = dao.find(hql1);
		List list2 = dao.find(hql2);
		List list3 = dao.find(hql3);
		List list4 = dao.find(hql4);
		
		if(list1.size() == 0 && list2.size() == 0 && list3.size() == 0 && list4.size() == 0){
			if( sysFunc.getMenuCodeGroup().length() <= 10){
				
				dao.save(sysFunc);
				return SUCCESS;//1
			}else{
				return FAIL_5;//0
			}
			
		}else if(list1.size() != 0){
			return FAIL_1;//子菜单代码重复2
		}else if(list2.size() != 0){
			return FAIL_2;//子菜单名称重复3
		}else if(list3.size() != 0){
			return FAIL_3;//子菜单模块路径重复4
		}else if(list4.size() != 0){
			return FAIL_4;//子菜单模块路径重复4
		}else{
			return FAIL;//0
		}
		
	}

	@Override
	public char deleteSysFunc(SysFunc sysFunc)throws Exception {
		SysFunc sysf=(SysFunc)dao.find(SysFunc.class, sysFunc.getFuncId());
		
		String sysFuncId = sysf.getFuncId();
		
		String hql = "from SysFunc where menuCodeGroup like '"+sysf.getMenuCodeGroup()+"%'";
		List list = dao.find(hql);
		if(list.size() > 1){
			return FAIL;//此菜单下如还有子菜单节点,则不能删除该节点
		}else {
			dao.delete(sysf);
			
			List<BaseRole> roles = dao.find("from BaseRole where 1=1 order by roleOrder");
			
			for(BaseRole role:roles){
				
				String funcIdGroup = role.getFuncIdGroup();
				
				if(StringUtils.isNotBlank(funcIdGroup) && funcIdGroup.indexOf(sysFuncId) != -1){
					
					funcIdGroup = funcIdGroup.replace(sysFuncId+",", "");
					
					role.setFuncIdGroup(funcIdGroup);
					
					dao.update(role);
				}
			}
			return SUCCESS;
		}
	}

	@Override
	public List<SysMenuNode> menuSelectList() {
		List<SysMenuNode> nodes = new ArrayList<SysMenuNode>();
		
		List<SysMenu>  sysMenus = new ArrayList<SysMenu>();
		sysMenus = dao.find("from SysMenu where 1=1 order by menuCode");
		for(SysMenu sysMenu:sysMenus){
			SysMenuNode sysMenuNode = new SysMenuNode();
			sysMenuNode.setNodeCode(sysMenu.getMenuCode());
			sysMenuNode.setNodeName(sysMenu.getMenuName());
			nodes.add(sysMenuNode);
			
			String hql = "from SysFunc where menuCodeGroup like '"+sysMenu.getMenuCode()+"%' and funcCode like '@%' order by menuCodeGroup";
			List<SysFunc> funcs = dao.find(hql);
			for(SysFunc sysFunc:funcs){
				SysMenuNode sysMenuNode1 = new SysMenuNode();
				sysMenuNode1.setNodeCode(sysFunc.getMenuCodeGroup());
				sysMenuNode1.setNodeName("&nbsp;&nbsp;&nbsp;&nbsp;"+sysFunc.getFuncName());
				nodes.add(sysMenuNode1);
			}
		}
		
		return nodes;
	}

	@Override
	public char modifySysFunc(SysFunc sysFunc)throws Exception {
		String hql1 = "from SysFunc where menuCodeGroup ='"+sysFunc.getMenuCodeGroup()+"' and funcId != '"+sysFunc.getFuncId()+"'";
		String hql2 = "from SysFunc where funcName ='"+sysFunc.getFuncName()+"' and funcId != '"+sysFunc.getFuncId()+"'";
		String hql3 = "from SysFunc where funcCode ='"+sysFunc.getFuncCode()+"' and funcId != '"+sysFunc.getFuncId()+"'";
		String hql4 = "from SysFunc where funcNameEn ='"+sysFunc.getFuncNameEn()+"' and funcId != '"+sysFunc.getFuncId()+"'";
		List list1 = dao.find(hql1);
		List list2 = dao.find(hql2);
		List list3 = dao.find(hql3);
		List list4 = dao.find(hql4);
		
		if(list1.size() == 0 && list2.size() == 0 && list3.size() == 0 && list4.size() == 0){
			
			SysFunc sysFunc1 = (SysFunc)dao.find(SysFunc.class, sysFunc.getFuncId());
			sysFunc1.setFuncCode(sysFunc.getFuncCode());
			sysFunc1.setFuncName(sysFunc.getFuncName());
			sysFunc1.setFuncNameEn(sysFunc.getFuncNameEn());
			sysFunc1.setMenuCodeGroup(sysFunc.getMenuCodeGroup());
			dao.update(sysFunc1);
			return SUCCESS;
		}else if(list1.size() != 0){
			return FAIL_1;//子菜单代码重复
		}else if(list2.size() != 0){
			return FAIL_2;//子菜单名称重复
		}else if(list3.size() != 0){
			return FAIL_3;//子菜单模块路径重复
		}else if(list4.size() != 0){
			return FAIL_4;//子菜单模块路径重复
		}else{
			return FAIL;
		}
	}

	@Override
	public List<SysFunc> sysFuncList(String menuId) {
		// TODO Auto-generated method stub
		SysMenu sysMenu = (SysMenu) dao.find(SysMenu.class, menuId);
		
		String hql = "from SysFunc where menuCodeGroup like '"+sysMenu.getMenuCode()+"__' order by menuCodeGroup";
		 
		List<SysFunc> sysFuncs = dao.find(hql);
	
		if(sysFuncs.size()>0){
			for(SysFunc sysFunc:sysFuncs){
				String hql2 = "from SysFunc where menuCodeGroup like '"+sysFunc.getMenuCodeGroup()+"__' order by menuCodeGroup";
				
				List<SysFunc> list2 = dao.find(hql2);
				sysFunc.setSysFuncs(list2);
			}
		}
		return sysFuncs;
	}

	@Override
	public SysFunc findFuncById(String funcId) {
		
		SysFunc sysFunc = new SysFunc();
		
		sysFunc = (SysFunc)dao.find(SysFunc.class, funcId);
		return sysFunc;
	}

	@Override
	public List findFuncByCode(String[] menuCodeGroup) {
		// TODO Auto-generated method stub
		List list1 = new ArrayList();
		
		for(int i=0;i<menuCodeGroup.length;i++){
			
			String hql = "from SysFunc where menuCodeGroup ='"+menuCodeGroup[i]+"'";
			List list = dao.find(hql);
			if(list.size()>0){
				SysFunc sysFunc = (SysFunc)list.get(0);
				String code = sysFunc.getFuncCode();
				if(code.indexOf("index") != -1){
				code = code.substring(0,code.lastIndexOf("index")-1);
				
				int count=0;
				if(list1.size()>0){
					for(int j=0;j<list1.size();j++){
						if(list1.get(j) == code){
							count++;
							break;
						}
					}
					if(count == 0){
						
						list1.add(code);
					}
					
				}else{
					list1.add(code);
				}
				
				}
			}
		}
		
		return list1;
		
	}

	@Override
	public List findFuncByFuncIds(String[] funcIds) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SysFunc> childSysFuncList(SysFunc sysFunc) {
		// TODO Auto-generated method stub
		
		try{
			String hql = "from SysFunc where menuCodeGroup like '"+sysFunc.getMenuCodeGroup()+"__' order by menuCodeGroup";
			
			List<SysFunc> childFuncList = new ArrayList();
			
			childFuncList = dao.find(hql);
			
			return childFuncList;
		}catch (Exception e) {
			return new ArrayList();
		}
	}

	@Override
	public SysFunc findByFuncCode(String code) {
		// TODO Auto-generated method stub
		try{
			SysFunc sysFunc = new SysFunc();
			
			String hql = "from SysFunc where menuCodeGroup='"+code+"'";
			
			List list = dao.find(hql);
			
			if(list!=null && list.size()>0){
				
				sysFunc = (SysFunc)list.get(0);
			}
			return sysFunc;
		}catch (Exception e) {
			// TODO: handle exception
			return new SysFunc();
		}
	}

}
